package com.baoji.auth.filter;

import com.alibaba.fastjson.JSONObject;
import com.baoji.auth.model.AccountErrorCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: ghm
 * @Date: 2021/09/24/17:11
 * @Description: 自定义类 用于进行匿名用户访问资源时无权限的处理
 */
public class JWTAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

        // 返回json
        response.setContentType("application/json; charset=UTF-8");

        // 状态码
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

        // 写出 :该账户暂无登录凭证，请登录
        response.getWriter().print(JSONObject.toJSONString(AccountErrorCode.CREDENTIALS_EXPIRED.getMessage()));

    }
}
